'use strict';

import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('Mjs1List');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const GET_LIST_START = ns('GET_LIST_START');
export const GET_LIST_SUCCESS = ns('GET_LIST_SUCCESS');
export const GET_MJS_SUCCESS = ns('GET_MJS_SUCCESS');
export const CLOSE_MJS_DIALOG = ns('CLOSE_MJS_DIALOG');
export const REMOVE_MJS = ns('REMOVE_MJS');
export const LOAD_ERR = ns('LOAD_ERR');
export const UPDATE_ZHEKOU_DONE = ns('UPDATE_ZHEKOU_DONE');
export function closeDialog() {
	  return {type: CLOSE_DIALOG,visible:false};
	}
export function closeMjsDialog(){
	return {type: CLOSE_MJS_DIALOG};
}
export function removeMjs(params) {
	  return (dispatch) => {
	    //发送请求前
	    dispatch({
	      type: GET_LIST_START
	    });
	    //接收到数据
	    ajax({
	      api: 'removeMjs',
	      method: 'GET',
	      data:params
	    },(json)=>{
	    	ajax({
	  	      api: 'removeMjsFromDatabase',
	  	      method: 'GET',
	  	      data:params
	  	    },(json)=>{
	  	    	dispatch({
	  	            type: REMOVE_MJS,
	  	            id: params.id
	  	          });
	  	    },(json)=>{
	  	    	const login = (json.code==210);
	  			dispatch({
	  				type: LOAD_ERR,
	  				message:json.msg+(json.sub_msg?json.sub_msg:""),
	  				loginFalse:login
	  			});
	  	    });
	    },(json)=>{
	    	if(json.sub_code=="isv.activity-not-exist:activityId"){
	    		ajax({
	  	  	      api: 'removeMjsFromDatabase',
	  	  	      method: 'GET',
	  	  	      data:params
	  	  	    },(json)=>{
	  	  	    	dispatch({
	  	  	            type: REMOVE_MJS,
	  	  	            id: params.id
	  	  	          });
	  	  	    },(json)=>{
	  	  	    	const login = (json.code==210);
	  	  			dispatch({
	  	  				type: LOAD_ERR,
	  	  				message:json.msg+(json.sub_msg?json.sub_msg:""),
	  	  				loginFalse:login
	  	  			});
	  	  	    });
	    	}else{
		    	const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
	    	}
	    });

	  };
	}
export function getMjsList(page,page_size,participate_range) {
  return (dispatch) => {
    //发送请求前
    dispatch({
      type: GET_LIST_START
    });
    //接收到数据
    ajax({
      api: 'getMjsList',
      method: 'GET',
      data:{page,page_size,participate_range}
    },(json)=>{
    	dispatch({
            type: GET_LIST_SUCCESS,
            data: json.data,
            total:parseInt(json.total)
          });
    },(json)=>{
    	const login = (json.code==210);
		dispatch({
			type: LOAD_ERR,
			message:json.msg+(json.sub_msg?json.sub_msg:""),
			loginFalse:login
		});
    });

  };
}
export function getMjs(id){
	  return (dispatch) => {
		    //发送请求前
		    dispatch({
		      type: GET_LIST_START
		    });
		    //接收到数据
		    ajax({
		      api: 'getMjs',
		      method: 'GET',
		      data:{id}
		    },(json)=>{
		    	dispatch({
		            type: GET_MJS_SUCCESS,
		            data: json.data,
		          });
		    },(json)=>{
		    	const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
		    });

		  };
}

export function updateMjs(p){
	let exclude_area_id = [];
	let exclude_area_name = [];
	if(p.exclude_area){
		p.exclude_area.map(area=>{
			exclude_area_id.push(area.id);
			exclude_area_name.push(area.name);
		});
	}
	const params={
			db_id:p.id,
			activity_id:p.activity_id,
			name:p.name,
			type:1,
			participate_range:p.participate_range,
			start_time:(typeof p.start_time=="object")?(p.start_time.getFullYear()+"-"+(p.start_time.getMonth()>=1&&p.start_time.getMonth()<=9?0:"")+(p.start_time.getMonth()+1)+"-"+(p.start_time.getDate()<=9?"0":"")+p.start_time.getDate()+" "+(p.start_time.getHours()<=9?"0":"")+p.start_time.getHours()+":"+(p.start_time.getMinutes()<=9?"0":"")+p.start_time.getMinutes()+":"+(p.start_time.getSeconds()<=9?"0":"")+p.start_time.getSeconds() ):p.start_time,
			end_time:(typeof p.end_time=="object")?(p.end_time.getFullYear()+"-"+(p.end_time.getMonth()>=1&&p.end_time.getMonth()<=9?0:"")+(p.end_time.getMonth()+1)+"-"+(p.end_time.getDate()<=9?"0":"")+p.end_time.getDate()+" "+(p.end_time.getHours()<=9?"0":"")+p.end_time.getHours()+":"+(p.end_time.getMinutes()<=9?"0":"")+p.end_time.getMinutes()+":"+(p.end_time.getSeconds()<=9?"0":"")+p.end_time.getSeconds() ):p.end_time,
			is_amount_over:p.is_amount_over,
			total_price:p.total_price?p.total_price*100:"0",
			is_amount_multiple:p.is_amount_multiple,
			is_item_count_over:p.is_item_count_over,
			item_count:p.item_count,
			is_item_multiple:p.is_item_multiple,
			is_shop_member:p.is_shop_member,
			shop_member_level:p.shop_member_level,
			is_user_tag:'false',
			user_tag:'',
			is_decrease_money:p.is_decrease_money,
			decrease_amount:p.decrease_amount?p.decrease_amount*100:"0",
			is_discount:p.is_discount,
			discount_rate:p.discount_rate?p.discount_rate*100:"0",
			is_send_gift:p.is_send_gift,
			gift_name:p.gift?p.gift[0].title:"",
			gift_id:p.gift?p.gift[0].num_iid:"0",
			gift_url:p.gift?"http://item.daily.taobao.net/item.htm?id="+p.gift[0].num_iid:"",
			gift_pic_url:p.gift?p.gift[0].pic_url:"",
			is_free_post:p.is_free_post,
			exclude_area:exclude_area_id.length>0?(exclude_area_id.join("*")+"#"+exclude_area_name.join("*")):""
		}

	return (dispatch) => {
	    //发送请求前
		dispatch({
		      type: GET_LIST_START
		    });
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.mjs.activity.add',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
				  api: "updateMjs",
				  method: 'POST',
				  data:params
				},(json) => {
					ajax({
						  api: 'saveMjsToDatabase',
						  method: 'POST',
						  data:{...params,...json}
						},(json_db) => {
							dispatch({
								type: CLOSE_MJS_DIALOG
								});
						},(json)=>{
							//(json.code==53) W2超时
							  dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:false
								});
						}	
					);
				},(json)=>{
					//(json.code==53) W2超时
					  dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:false
						});
				}	
			);
		}
	}
}
